#include<iostream>
#include<vector>
using namespace std;

class Solution
{
public:
    void printMatrix(const vector<vector<int>>& matrix) {
        int ar = 0;
        int ac = 0;
        int br = 0;
        int bc = 0;
        int row = matrix.size();
        int col = matrix[0].size();

        // cout << "row:" << row << " col:" << col << endl;
        bool aTob = false;
        while(ar != row) {
            // cout << "print:" << ar << " " << ac << endl;
            // cout << "print:" << br << " " << bc << endl;
            //打印(ar,ac) -> (br,bc)
            printLevel(matrix, ar, ac, br, bc, aTob);
            cout << endl;
            //这里有一个细节 以谁为基准 谁写在后面
            //(ar,ac) 向右 向下走
            ar = (ac == col-1) ? ar + 1 : ar;
            ac = (ac == col-1) ? ac : ac + 1;
            //(br,bc) 向下 向右走
            bc = (br == row-1) ? bc + 1 : bc;
            br = (br == row-1) ? br : br + 1;
            aTob = !aTob;
        }
    }

    void printLevel(const vector<vector<int>>& matrix, int ar, int ac, int br, int bc, bool aTob) {
        if(aTob) {
            //a -> b
            while(ar != br + 1) {
                cout << matrix[ar++][ac--] << " ";
            }
        } else {
            //b -> a
            while(br != ar - 1) {
                cout << matrix[br--][bc++] << " ";
            }
        }
    }

    void test() {
        vector<vector<int>> v = {{1,2,3},{4,5,6},{7,8,9}};
        printMatrix(v);
    }
};

int main() {
    Solution().test();
    return 0;
}